***************************************
**** 	Table 1:	MAIN BALANCE TABLE
***************************************

use "$dir/Data/analysis_main.dta", clear

keep if survsample==1 

cap file close sumstat
file open sumstat using "$om/$t/Table_1.tex", write replace
		file write sumstat "{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}  \begin{tabular}{@{\extracolsep{0pt}}p{7cm}*{6}{>{\centering\arraybackslash}m{2.2cm}}@{}}   \toprule" _n
		file write sumstat " &\multicolumn{1}{c}{Control Mean}&\multicolumn{1}{c}{Coef. on Cash}&\multicolumn{1}{c}{P-value} \\" _n ///
		" & (1) & (2) & (3)  \\" _n ///
		"\midrule" _n ///
		"\multicolumn{4}{l}{\textit{Panel A. Demographics, Labor, and Wealth}} \\ \addlinespace[5pt]  " _n 

	// panel A
	
	foreach var of varlist age_m educ read_odiya_m married anychildren caslabor_m bs_labor_7 bs_labor_30 richhouse_m ownland_m hasnocredits_m canget1k_m wealth_ind_avg{ 
	
		local varlab: variable label `var'
		
		reghdfe `var' cash if pid1==1, absorb(roundgroup) cluster(pid)
		
		local coef_treat1: di %9.3f _b[cash]
		local se_treat1: di %9.3f _se[cash]
		local se_treat1 = strtrim("`se_treat1'")
		
		local t1=(_b[cash])/(_se[cash])
		local p1=2*ttail(e(df_r), abs(`t1'))	
			if `p1'<=.10 local stars1 "*"
			if `p1'<=.05 local stars1 "**"
			if `p1'<=.01 local stars1 "***"
			if `p1'>.10 local stars1 ""		
		local pval1: di %9.3f `p1'
		
		summ `var' if e(sample) & cash==0
		local mean_cont1: di %9.3f r(mean)
		local sd_cont1: di %9.3f r(sd)
		local sd_cont1 = strtrim("`sd_cont1'")
		
		file write sumstat "`varlab' & `mean_cont1' & `coef_treat1' & `pval1'`stars1'  \\  " _n  
		file write sumstat "		 &  [`sd_cont1']  & (`se_treat1') &   \\     \addlinespace[2pt]   " _n  

	}
	// panel B
	
	file write sumstat "\addlinespace[4pt] \multicolumn{4}{l}{\textit{Panel B. Financial Worries and Loans}} \\   \addlinespace[5pt]  " _n 
				
	foreach var of varlist worry1_m anyloans_worry bl_loanamt_m hasloan loan_moneylender  { 
		
		local varlab: variable label `var'
		
		reghdfe `var' cash if pid1==1, absorb(roundgroup) cluster(pid)
	
		local coef_treat1: di %9.3f _b[cash]
		local se_treat1: di %9.3f _se[cash]
		local se_treat1 = strtrim("`se_treat1'")
		
		local t1=(_b[cash])/(_se[cash])
		local p1=2*ttail(e(df_r), abs(`t1'))	
			if `p1'<=.10 local stars1 "*"
			if `p1'<=.05 local stars1 "**"
			if `p1'<=.01 local stars1 "***"
			if `p1'>.10 local stars1 ""		
		local pval1: di %9.3f `p1'
	
		summ `var' if e(sample) & cash==0
		local mean_cont1: di %9.3f r(mean)
		local sd_cont1: di %9.3f r(sd)
		local sd_cont1 = strtrim("`sd_cont1'")
		
		if "`var'"=="bl_loanamt_m"{
			foreach y in mean_cont1 sd_cont1 coef_treat1 se_treat1{
				local `y'=trim(string(``y'', "%9.0fc"))
			}
		}			
		
		file write sumstat "`varlab' & `mean_cont1' & `coef_treat1' & `pval1'`stars1'  \\  " _n  
		file write sumstat "		 &  [`sd_cont1']  & (`se_treat1') &   \\     \addlinespace[2pt]   " _n  

	}

	// panel C
	
	file write sumstat "\addlinespace[4pt] \multicolumn{4}{l}{\textit{Panel C. Baseline Attendance and Productivity}} \\  \addlinespace[5pt]" _n 
	
	foreach var of varlist attend productivity n_productivity att_ind { 
	
		local varlab: variable label `var'
	
		if "`var'"=="attend" reghdfe `var' cash if pidday1==1 & postcash==0, absorb(roundgroup) cluster(pid)
		else reghdfe `var' cash if postcash==0, absorb(roundgroup) cluster(pid)
		
		local coef_treat1: di %9.3f _b[cash]
		local se_treat1: di %9.3f _se[cash]
		local se_treat1 = strtrim("`se_treat1'")
		
		local t1=(_b[cash])/(_se[cash])
		local p1=2*ttail(e(df_r), abs(`t1'))	
			if `p1'<=.10 local stars1 "*"
			if `p1'<=.05 local stars1 "**"
			if `p1'<=.01 local stars1 "***"
			if `p1'>.10 local stars1 ""		
		local pval1: di %9.3f `p1'
		
		summ `var' if e(sample) & cash==0
		local mean_cont1: di %9.3f r(mean)
		local sd_cont1: di %9.3f r(sd)
		local sd_cont1 = strtrim("`sd_cont1'")
		
		file write sumstat "`varlab' & `mean_cont1' & `coef_treat1' & `pval1'`stars1'  \\  " _n  
		file write sumstat "		 &  [`sd_cont1']  & (`se_treat1') &   \\     \addlinespace[2pt]   " _n  

	}
		
	count if cash==0 & pid1==1
	local N1 = r(N)
	count if cash==1 & pid1==1
	local N2 = r(N)	

	
	file write sumstat "\midrule \addlinespace[5pt]   " _n  
	file write sumstat "N: workers (Control or Cash) & `N1' & `N2'    \\  " _n   
	file write sumstat "\bottomrule   \end{tabular}   }" _n           // table footer
	file close sumstat	


